package mylibrary.io.jichu;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.Writer;

public class Class1 {
	
	public static void main(String[] args) throws Exception {
		
		Class1 class1 = new Class1();
		//class1.createFile("d:\\hello.txt");
		
		//System.out.println(File.separator);
		//System.out.println(File.pathSeparator);
		
		//class1.createFile("D:" + File.separator + "hello.txt");
		
		//class1.delete("D:" + File.separator + "hello.txt");
		
		//class1.makeDir("D:" + File.separator + "hello.txt");
		
		//class1.listFiles("D:" + File.separator + "Movie");
		
		//System.out.println(class1.isDir("D:" + File.separator + "output.log"));
		
		//class1.listAllFiles("D:" + File.separator + "Movie");
		
//		RandomAccessFile demo = class1.writeRandomAccessFile("d:\\random.dat");
//		demo.seek(0);
//		System.out.println(demo.readChar());
//		System.out.println(demo.readByte());
//		System.out.println(demo.readByte());
//		System.out.println(demo.readByte());
//		System.out.println(demo.readInt());
//		System.out.println(demo.readBoolean());
		
		//class1.writeStr2File("d:\\test.txt", "你好");
		
		//class1.addContent2File("d:\\test.txt", "\n大家好");   //换行   若用 notepad 查看，需要  \r\n
		
//		byte[] b = class1.readContent("d:\\test.txt");
//		System.out.println(new String(b));
		
		//byte[] b = class1.readBigFile("D:\\Study\\hibernate-3.2.6-src\\lgpl.txt");
		
		char[] ch = class1.readTextFile("d:\\test.txt");
		System.out.println(new String(ch, 0, ch.length));
		
	}

	public void createFile(String filePath) throws Exception {
		
		File f = new File(filePath);
		f.createNewFile();
	}
	
	public void delete(String filePath) {
		
		File f = new File(filePath);
		if(f.exists()) {
			f.delete();	
		} else {
			System.out.println("File doesn't exist");
		}
	}
	
	public void makeDir(String dirPath)  {
		File f = new File(dirPath);
		f.mkdir();
	}
	
	public void listFiles(String dirPath) {
		File f = new File(dirPath);
		String[] list = f.list();		//列出文件名
		for(int i=0; i<list.length; i++) {
			System.out.println(list[i]);
		}
		
		File[] files = f.listFiles();
		for(int i=0; i<list.length; i++) {
			System.out.println(files[i]);
		}
	}
	
	public boolean isDir(String dirPath) {
		return new File(dirPath).isDirectory();
	}
	
	public void listAllFiles(String dirPath) {
		
		File file = new File(dirPath);
		if(file.isDirectory()) {
			
			File[] files = file.listFiles();
			for(int i=0; i<files.length; i++) {
				listAllFiles(files[i].getAbsolutePath());
			}
		} else {
			//System.out.println(file.getAbsolutePath());
			System.out.println(file);
		}
	}
	
	public RandomAccessFile writeRandomAccessFile(String filePath) throws Exception {
		File file = new File(filePath);
		
		if(!file.exists()) {
			RandomAccessFile demo = new RandomAccessFile(file, "rw");
			demo.writeChar('人');		//2 byte
			demo.writeBytes("asd");		//3 byte
			demo.writeInt(12);			//4 byte
			demo.writeBoolean(true);	//1 byte
			demo.writeChar('a');		//2 byte
			demo.writeFloat(1.2f);		//4 byte
			demo.writeDouble(111.123);  //8 byte
			return demo;
		}
		return new RandomAccessFile(file, "rw");
	}
	
	/**
	 * 向文件中写入字符串
	 */
	
	public void writeStr2File(String filePath, String str) throws Exception {
		
		File file = new File(filePath);
		
		//Writer fw = new FileWriter(file);
		//fw.write(str);
		//fw.flush();
		
		OutputStream os = new FileOutputStream(file);
		os.write(str.getBytes());
		os.close();
	}
	
	/**
	 * 向文件中追加内容
	 */
	
	public void addContent2File(String filePath, String str) throws Exception {
		
		File file = new File(filePath);
		
		//OutputStream os = new FileOutputStream(file, true);
		//os.write(str.getBytes());
		//os.close();
		
		Writer writer = new FileWriter(file, true);
		writer.write(str);
		writer.close();
	}
	
	/**
	 * 读取文件内容
	 */
	
	public byte[] readContent(String filePath) throws Exception {
		File f = new File(filePath);
		
		InputStream is = new FileInputStream(f);
		byte[] b = new byte[(int)f.length()];
		is.read(b);
		is.close();
		
		return b;
	}
	
	/**
	 * 读取大文件内容
	 */
	
	public byte[] readBigFile(String filePath) throws Exception {
		File f = new File(filePath);
		
		InputStream is = new FileInputStream(f);
		
		File out = new File("d:\\11.txt");
		OutputStream os = new FileOutputStream(out);
		
//		int temp=0;
//		while((temp=is.read()) != -1) {
//			os.write(temp);
//		}			
		
		byte[] buff = new byte[1024];
		int reader = -1;
		int i = 0;
		while((reader=is.read(buff)) != -1) {
			os.write(buff, 0, reader);
			System.out.println(i++);
		}
 		
		is.close();
		os.close();
		
		return null;
	}
	
	/**
	 * 读取文本文件内容
	 */
	public char[] readTextFile(String filePath) throws Exception {
		File f = new File(filePath);
		Reader reader = new FileReader(f);
		
		char[] ch = new char[100];
		int count = reader.read(ch);
		reader.close();
		System.out.println("The text length is " + count);
		return ch;
	}
	
}
